package com.kalua.list.dbaccess.datamgmt;

import org.springframework.cassandra.core.RowMapper;

import com.datastax.driver.core.Row;
import com.datastax.driver.core.exceptions.DriverException;
import com.kalua.list.domainmodel.SequenceValue;
import com.kalua.list.domainmodel.SequenceValueImpl;

public class SequenceRowMapper implements RowMapper<SequenceValue>{

	public static final String SEQUENCE_COL_FAMILY_NAME = "sequence";
	
	public static final String NAME_COL_NAME = "name"; 
	public static final String VALUE_COL_NAME = "value";
	public static final String INCREMENT_COL_NAME = "increment";

	public static final int DEFAULT_INCREMENT = 1;	
	
	@Override
	public SequenceValue mapRow(Row row, int rowNum) throws DriverException {
		SequenceValue result;
		long value = row.getLong(VALUE_COL_NAME);
		int increment;
		if (!row.isNull(INCREMENT_COL_NAME))
			increment = row.getInt(INCREMENT_COL_NAME);
		else increment = DEFAULT_INCREMENT;
		result = new SequenceValueImpl(value, value+increment);
		return result;
	}
}